﻿using Microsoft.Vbe.Interop;
using CommandBar = Microsoft.Office.Core.CommandBar;
using CommandBarButton = Microsoft.Office.Core.CommandBarButton;
using MsoButtonStyle = Microsoft.Office.Core.MsoButtonStyle;
using MsoControlType = Microsoft.Office.Core.MsoControlType;
namespace MyCusTools
{
    class VbeRightMenu_Cls : IExcelAddIn
    {
        private CommandBar MenuBar; // 和插件同声明周期
        public void AutoClose()
        {
            //throw new NotImplementedException();
        }

        public void AutoOpen()
        {
            dynamic ExcelApp = ExcelDnaUtil.Application;
            VBE Vbe = ExcelApp.VBE;
            MenuBar = Vbe.CommandBars["Code Window"];
            CommandBarButton Button = (CommandBarButton)MenuBar.Controls.Add(
                MsoControlType.msoControlButton,
                Before: 1);

            Button.Style = MsoButtonStyle.msoButtonCaption;
            Button.Caption = "右键自定义按钮"; // 显示的Text
            Button.Tag = "Code Window"; // 所属的Tag
            Button.Click += static (CommandBarButton ctrl, ref bool cancel) =>
            {
                CodeSave_WinForm CodeSave = new CodeSave_WinForm();
                CodeSave.Visible = !CodeSave.Visible;
            };
        }
    }
}
